package com.ys.otms.utils;

import com.ys.otms.constant.ThirdConstant;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.text.SimpleDateFormat;

public class ShipDtAPIUtil {

    //获取子key
    public static String getComKey() {
        String reqUrl = ThirdConstant.DT_GET_COM_URL;
        JSONObject jsonObject = HttpUtil.get(reqUrl);
        if ("0".equals(jsonObject.getString("status"))) {
            return jsonObject.getString("seedStr");
        }
        return "";
    }

    //船舶基本信息及最新船位查询服务
    public static JSONObject queryShip(JSONObject obj) {
        String seedStr = getComKey();
        if (StringUtil.isEmpty(seedStr)) {
//            objRet.put("msg","查询子key失败！");
//            objRet.put("SUCCESS",false);
//            return objRet;
            return null;
        }
        String kw = JsonUtil.getJSONObjectString(obj, "keyword");
        if (StringUtil.isEmpty(kw)) {
//            objRet.put("SUCCESS",false);
//            objRet.put("msg","查询条件为空！");
//            return objRet;
            return null;
        }

        String key = EncryptionUtil.md5(seedStr + ThirdConstant.DT_KEY);
        String url = ThirdConstant.DT_QUERY_URL.replaceAll("keyword", kw).replaceAll("DT_KEY", key);

        JSONObject jsonObject = HttpUtil.get(url);
        String status = jsonObject.getString("status");
        if ("0".equals(status) || "8".equals(status)) {
            JSONArray jsonArray = jsonObject.getJSONArray("result");
            if (jsonArray.size() == 0) {
                return null;
            }
            return jsonObject;
        }
//        objRet.put("SUCCESS",false);
//        objRet.put("MSG","返回数据失败，status：" + status);
//        return objRet;
        return null;
    }

    //单一/批量，批量shipIds最多不超过 20 条
    public static JSONObject getShips(JSONObject obj) {
        JSONObject objRet = new JSONObject();
        String shipIds = JsonUtil.getJSONObjectString(obj, "shipIds");
        if(StringUtil.isEmpty(shipIds)) {
            return null;
        }
        String seedStr = getComKey();
        if (StringUtil.isEmpty(seedStr)) {
            return null;
        }
        String key = EncryptionUtil.md5(seedStr + ThirdConstant.DT_KEY);
        String url = ThirdConstant.DT_GET_SHIP_URL.replaceAll("DT_KEY", key);
        String[] shipIdsArr = shipIds.split(",");
        JSONArray jsonArray = new JSONArray();
        if (shipIdsArr.length > 20) {
            StringBuffer sb = new StringBuffer();
            for(int i = 0; i < shipIdsArr.length; i++) {
                if (i%20 == 0 && i != 0) {
                    String reqUrl = url.replaceAll("SHIP_IDS",sb.substring(0,sb.length() - 1));
                    objRet = HttpUtil.get(reqUrl);
                    jsonArray.addAll(JsonUtil.getJSONArray(objRet,"result"));
                    sb = new StringBuffer();
                }
                sb.append(shipIdsArr[i]).append(",");
            }
            if (sb.length() > 0) {
                String reqUrl = url.replaceAll("SHIP_IDS",sb.substring(0,sb.length() - 1));
                objRet = HttpUtil.get(reqUrl);
                jsonArray.addAll(JsonUtil.getJSONArray(objRet,"result"));
            }
            objRet = new JSONObject();
            objRet.put("data", jsonArray);
        }
        else {
            url = url.replaceAll("SHIP_IDS",shipIds);
            objRet.put("data", HttpUtil.get(url).getJSONArray("result"));
        }
        return objRet;
    }


    //船舶历史航次查询服务（最多查询过去 4 个月的历史航次）  航行日志
    public static JSONObject getShipHistoryVoyage(JSONObject obj) throws Exception {
        JSONObject objRet;
        String shipId = JsonUtil.getJSONObjectString(obj,"shipId");
        if (StringUtil.isEmpty(shipId)) {
            return null;
        }
        String begin = JsonUtil.getJSONObjectString(obj,"begin");
        String end = JsonUtil.getJSONObjectString(obj,"end");
        if (StringUtil.isEmpty(begin) || StringUtil.isEmpty(end)) {
            return null;
        }

        String seedStr = getComKey();
        if (StringUtil.isEmpty(seedStr)) {
            return null;
        }
        String key = EncryptionUtil.md5(seedStr + ThirdConstant.DT_KEY);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Long beginTime = sdf.parse(begin).getTime()/1000;
        Long endTime = sdf.parse(end).getTime()/1000;
        String reqUrl = ThirdConstant.DT_GET_SHIP_HISTORY_VOYAGE_URL.replaceAll("DT_KEY", key).replaceAll("SHIP_ID",shipId)
                .replaceAll("BEGIN_TIME",beginTime.toString()).replaceAll("END_TIME",endTime.toString());
        objRet = HttpUtil.get(reqUrl);
        return objRet;
    }

    //船舶历史轨迹查询服务（最多查询过去 4 个月的历史轨迹）  航行轨迹
    public static JSONObject getShipHistoryTrack(JSONObject obj) throws Exception {
        JSONObject objRet;
        String shipId = JsonUtil.getJSONObjectString(obj,"shipId");
        if (StringUtil.isEmpty(shipId)) {
            return null;
        }
        String begin = JsonUtil.getJSONObjectString(obj,"begin");
        String end = JsonUtil.getJSONObjectString(obj,"end");
        if (StringUtil.isEmpty(begin) || StringUtil.isEmpty(end)) {
            return null;
        }

        String seedStr = getComKey();
        if (StringUtil.isEmpty(seedStr)) {
            return null;
        }
        String key = EncryptionUtil.md5(seedStr + ThirdConstant.DT_KEY);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Long beginTime = sdf.parse(begin).getTime()/1000;
        Long endTime = sdf.parse(end).getTime()/1000;
        String reqUrl = ThirdConstant.DT_GET_SHIP_HISTORY_TRACK_URL.replaceAll("DT_KEY", key).replaceAll("SHIP_ID",shipId)
                .replaceAll("BEGIN_TIME",beginTime.toString()).replaceAll("END_TIME",endTime.toString());
        objRet = HttpUtil.get(reqUrl);
        return objRet;
    }
}
